export function ajaxRequest(url, method, params) {
  return new Promise((resolve, reject) => {
    let data;
    if (method === "POST" || method === "PUT") {
      data = JSON.stringify(params);
    } else {
      data = null;
    }
    $.ajax({
      type: method,
      url,
      data,
      contentType: "application/json;charset=utf-8",
      beforeSend: function(request) {
        if (localStorage.getItem("userInfo")) {
          request.setRequestHeader(
            "Authorization",
            JSON.parse(localStorage.getItem("userInfo")).token
          );
        }
      },
      success: (res) => {
        resolve(res);
      },
      error: (jqXHR, textStatus, errorThrown) => {
        if (jqXHR.status == 401) {
          alert('登录信息过期，请重新登陆')
        }
        reject(textStatus);
      },
    });
  });
}

// 登录
export const login = (params) => {
  return ajaxRequest("/user/login", "POST", params);
};

// 新建用户
export const addUser = (params) => {
  return ajaxRequest("/user/register", "POST", params);
};

// 获取用户列表
export const getUser = (params) => {
  return ajaxRequest(`/user/get`, "POST", params);
};

// getUserInfo
export const getUserInfo = (id) => {
  return ajaxRequest(`/user/${id}`, "GET");
};

export const hasAdmin = (params) => {
  return ajaxRequest(`/user/hasAdmin`, "POST", params);
}

// 删除用户
export const deleteUser = (id) => {
  return ajaxRequest(`/user/${id}`, "DELETE");
}

// 编辑用户
export const updateUser = (id, params) => {
  return ajaxRequest(`/user/${id}`, "POST", params);
};
export const updateUserP = (id, params) => {
  return ajaxRequest(`/user/pass/${id}`, "POST", params);
};
// 获取模板信息
export const getSetting = () => {
  return ajaxRequest(`/setting`, "GET");
}
// 编辑用户
export const updateSetting = (params) => {
  return ajaxRequest(`/setting`, "POST", params);
};

export const getRecord = () => {
  return ajaxRequest(`/record`, "GET");
}
export const getOrder = (params) => {
  return ajaxRequest(`/order?${params}`, "GET");
}

export const getMenu = (params) => {
  return ajaxRequest(`/menu/get`, "POST", params);
}
export const addMenu = (params) => {
  return ajaxRequest(`/menu/add`, "POST", params);
}
export const updateMenu = (id, params) => {
  return ajaxRequest(`/menu/${id}`, "POST", params);
}
export const deleteMenu = (id) => {
  return ajaxRequest(`/menu/${id}`, "DELETE");
}
export const addClose = (params) => {
  return ajaxRequest("/close", "POST", params);
};

export const updateClose = (id, params) => {
  return ajaxRequest(`/close/${id}`, "POST", params);
};


export const getQuestion = (params) => {
  return ajaxRequest(`/question/get`, "POST", params);
}
export const addQuestion = (params) => {
  return ajaxRequest("/question", "POST", params);
};

export const deleteQuestion = (id) => {
  return ajaxRequest(`/question/${id}`, "DELETE");
}

export const updateQuestion = (id, params) => {
  return ajaxRequest(`/question/update/${id}`, "POST", params);
};


export const getCourse = (params) => {
  return ajaxRequest(`/course/get`, "POST", params);
}
export const addCourse = (params) => {
  return ajaxRequest("/course", "POST", params);
};

export const deleteCourse = (id) => {
  return ajaxRequest(`/course/${id}`, "DELETE");
}

export const updateCourse = (id, params) => {
  return ajaxRequest(`/course/${id}`, "POST", params);
};


export const getPaper = (params) => {
  return ajaxRequest(`/paper/get`, "POST", params);
}
export const addPaper = (params) => {
  return ajaxRequest("/paper", "POST", params);
};

export const deletePaper = (id) => {
  return ajaxRequest(`/paper/${id}`, "DELETE");
}

export const updatePaper = (id, params) => {
  return ajaxRequest(`/paper/${id}`, "PUT", params);
};

export const queryQuestionsByIds = (params) => {
  return ajaxRequest(`/question/questionsByIds`, "POST", params);
};

export const getExam = (params) => {
  return ajaxRequest(`/exam/get`, "POST", params);
}
export const addExam = (params) => {
  return ajaxRequest("/exam", "POST", params);
};

export const deleteExam = (id) => {
  return ajaxRequest(`/exam/${id}`, "DELETE");
}

export const updateExam = (id, params) => {
  return ajaxRequest(`/exam/${id}`, "POST", params);
};


export const getWrong = (params) => {
  return ajaxRequest(`/wrong/get`, "POST", params);
}
export const addWrong = (params) => {
  return ajaxRequest("/wrong", "POST", params);
};

export const deleteWrong = (id) => {
  return ajaxRequest(`/wrong/${id}`, "DELETE");
}

export const updateWrong = (id, params) => {
  return ajaxRequest(`/wrong/${id}`, "POST", params);
};

export const download = (filename) => {
  return ajaxRequest(`/download/${filename}`, "POST", {});
};


export const autoPaper = (params) => {
  return ajaxRequest(`/paper/auto`, "POST", params);
};